import plotly.express as px
from project_tools.dataformat import *
# import cleaned-up tobacco average sales data
states_sales = tobacco_data_import()

# change format of data
sub = states_sales.groupby(['LocationDesc', 'LocationAbbr', 'DataValueUnit']).apply(lambda row: pd.Series({"2013": row.DataValue[row.YearStart == 2013].values[0],
                                                                   "2014": row.DataValue[row.YearStart == 2014].values[0],
                                                                   # ignoring 2015 because missing data in the original source
                                                                   "2016": row.DataValue[row.YearStart == 2016].values[0],
                                                                   "2017": row.DataValue[row.YearStart == 2017].values[0],
                                                                   "2018": row.DataValue[row.YearStart == 2018].values[0],
                                                                    "2019": row.DataValue[row.YearStart == 2019].values[0]}))

sub =sub.reset_index(['LocationDesc', 'LocationAbbr', 'DataValueUnit'], drop=False)
sub.head()
LocationDesc LocationAbbr DataValueUnit 2013 2014 2016 2017 2018 2019
0 Alabama AL pack sales per capita 64.6 61.7 60.1 58.1 54.7 53.1
1 Alaska AK pack sales per capita 39 37.2 35.6 32.9 31.1 30.4
2 Arizona AZ pack sales per capita 24.4 23 23.1 22.5 21.8 20.7
3 Arkansas AR pack sales per capita 57.5 54.4 54.2 52.4 50.2 47.6
4 California CA pack sales per capita 23.9 22.7 22 20.5 16.6 15.8
median_income = income_data_import()
# avg sales (sales per capita) for each year (6 in total)

# Year 2015 omitted
Years = [2013, 2014, 2016, 2017, 2018, 2019]
year = 0

# plotting 6 graphs in 2013, 2014, 2016, 2017, 2018, and 2019
for index in range(3, 9):
    
    ddf = sub.iloc[:, index].astype(float)
    
    fig =  px.choropleth(sub,
                         locations="LocationAbbr",
                         hover_name="LocationDesc",
                         color= ddf.values,
                         locationmode='USA-states',
                         color_continuous_scale='greens',
                         range_color = (80000, 40000))

    fig.update_layout(title_text = f"Sales per capita in {Years[year]}", geo_scope='usa')

    fig.write_html(f"output/avgSales{Years[year]}.html")
    year += 1
    #fig.show()

# show output plot for 2013
putHTML(year = 2013, avg_sales = True)
# median income graph for each year (6 in total)
Years1 = [2013, 2014, 2015, 2016, 2017, 2018, 2019]
year = 0

# plotting 7 graphs in 2013 to 2019
for index in reversed(range(1, 8)):
    ddf = median_income.iloc[:, index].astype(float)

    fig = px.choropleth(median_income,
                         locations="state",
                         hover_name="Location",
                         color=ddf.values,
                         locationmode='USA-states',
                         color_continuous_scale='reds',
                         range_color = (80000, 40000))

    fig.update_layout(title_text = f"Median Income in {Years1[year]}", geo_scope='usa')

    fig.write_html(f"output/medianIncome{Years1[year]}.html")
    year += 1
    #fig.show()

# show output plot for 2013
putHTML(year = 2013, avg_sales = False)